package com.ljd.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljd.entity.EduSubject;
import com.ljd.entity.excel.ExcelSubjectData;
import com.ljd.entity.subject.OneSubject;
import com.ljd.entity.subject.TwoSubject;
import com.ljd.listener.SubjectExcelListener;
import com.ljd.mapper.EduSubjectMapper;
import com.ljd.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author ljd
 * @since 2021-12-02
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, ExcelSubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    // 课程分类列表
//    @Override
//    public List<OneSubject> getAllSubject() {
//        // 查询所有的一级分类
//        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_id","0");
//        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper);
//        // 查询所有的二级分类
//        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
//        queryWrapper.ne("parent_id","0");
//        List<EduSubject> twoSubjects = baseMapper.selectList(queryWrapper);
//        // 创建集合用于存储最终封装数据
//        List<OneSubject> finalSubjects = new ArrayList<>();
//        // 封装一级分类
//        List<OneSubject> oneSubjectList = oneSubjects.stream().map(entity -> {
//            OneSubject subject1 = new OneSubject();
//            String oneId = entity.getId();
//            String oneTitle = entity.getTitle();
//            // 封装二级分类
//            List<TwoSubject> twoSubjectList = new ArrayList<>();
//            for (EduSubject twoSubject : twoSubjects) {
//                TwoSubject twoSubjectVo = new TwoSubject();
//                if(twoSubject.getParentId().equals(oneId)) {
//                    twoSubjectVo.setId(twoSubject.getId());
//                    twoSubjectVo.setTitle(twoSubject.getTitle());
//                    twoSubjectList.add(twoSubjectVo);
//                }
//            }
////            List<TwoSubject> twoSubjectList = twoSubjects.stream().map(entity2 -> {
////                if (entity2.getParentId().equals(oneId)) {
////                    TwoSubject subject2 = new TwoSubject();
////                    String twoId = entity2.getId();
////                    String twoTitle = entity2.getTitle();
////                    subject2.setId(twoId);
////                    subject2.setTitle(twoTitle);
////                    return subject2;
////                }
////            }).distinct().collect(Collectors.toList());
//            subject1.setId(oneId);
//            subject1.setTitle(oneTitle);
//            subject1.setChildren(twoSubjectList);
//            return subject1;
//        }).distinct().collect(Collectors.toList());
//        return oneSubjectList;
//    }
    @Override
    public List<OneSubject> getAllSubject() {
        // 查询所有的一级分类
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper);
        // 查询所有的二级分类
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("parent_id","0");
        List<EduSubject> twoSubjects = baseMapper.selectList(queryWrapper);
        // 创建集合用于存储最终封装数据
        List<OneSubject> finalSubjects = new ArrayList<>();
        // 封装一级分类
        for (EduSubject oneSubject : oneSubjects) {
            OneSubject subject01 = new OneSubject();
            BeanUtils.copyProperties(oneSubject,subject01);
            List<TwoSubject> finalTwo = new ArrayList<>();
            for (EduSubject twoSubject : twoSubjects) {
                if(twoSubject.getParentId().equals(oneSubject.getId())) {
                    TwoSubject subject02 = new TwoSubject();
                    BeanUtils.copyProperties(twoSubject,subject02);
                    finalTwo.add(subject02);
                }
            }
            subject01.setChildren(finalTwo);
            finalSubjects.add(subject01);
        }
        return finalSubjects;
    }
}